package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.NoteCourseBo;
import com.tpyule.tpclass.entity.bo.NoteDetailBo;
import com.tpyule.tpclass.entity.bo.NoteSelfBo;
import com.tpyule.tpclass.entity.dto.NoteQueryDto;
import com.tpyule.tpclass.entity.pojo.Note;
import com.tpyule.tpclass.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/16 12:26
 */
@RestController
@RequestMapping("/note")
public class NoteController extends BaseController{
    @Autowired
    NoteService noteService;
    @RequestMapping("/add")
    @ResponseBody
    public void addNote(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        String noteStr = request.getParameter("noteData");
        try {
            if (noteStr!=null){
                Note note = null;
                note = objectMapper.readValue(noteStr, Note.class);
                resultBean = noteService.addNote(note);
            }  else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getListNote")
    @ResponseBody
    public void getListNote(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        String noteQueryData = request.getParameter("noteQueryData");
        try {
            if (noteQueryData!=null){
                NoteQueryDto note = null;
                note = objectMapper.readValue(noteQueryData, NoteQueryDto.class);
                List<NoteDetailBo> notes = noteService.getListNote(note);
                if(notes!=null){
                    resultBean = new SuccessResultBean(notes,"获取成功");
                }else {
                    resultBean = new FailResultBean("获取失败");
                }
            }  else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getNoteCourse")
    @ResponseBody
    public void getNoteCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            if (userId!=null){
                List<NoteCourseBo> notes = noteService.getListInfo(userId);
                if(notes!=null){
                    resultBean = new SuccessResultBean(notes,"获取成功");
                }else {
                    resultBean = new FailResultBean("获取失败");
                }
            }  else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getSelfNote")
    @ResponseBody
    public void getSelfNote(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        String noteQueryData = request.getParameter("noteQueryData");
        try {
            if (noteQueryData!=null){
                NoteQueryDto note = null;
                note = objectMapper.readValue(noteQueryData, NoteQueryDto.class);
                List<NoteSelfBo> notes = noteService.getSelfNote(note);
                if(notes!=null){
                    resultBean = new SuccessResultBean(notes,"获取成功");
                }else {
                    resultBean = new FailResultBean("获取失败");
                }
            }  else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
